package com.controller;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.beans.Code;
import com.beans.Company;
import com.beans.User;
import com.service.SysManager;
import com.util.FileUtil;
import com.vo.Page;

public class SysController extends UniversalController {
	private SysManager sysManager;
	
	public SysManager getSysManager() {
		return sysManager;
	}

	public void setSysManager(SysManager sysManager) {
		this.sysManager = sysManager;
	}
	public ModelAndView getIndex(HttpServletRequest request,
			HttpServletResponse response) throws IllegalAccessException,
			InvocationTargetException {
		return new ModelAndView("index");
	}
	public ModelAndView getViewBaseInfo(HttpServletRequest request,
			HttpServletResponse response) throws IllegalAccessException,
			InvocationTargetException {
		return new ModelAndView("baseInfo");
	}
	public ModelAndView getHome(HttpServletRequest request,
			HttpServletResponse response) throws IllegalAccessException,
			InvocationTargetException {
		Map map=new HashMap();
		User u=(User)request.getSession().getAttribute("user");
		if(u!=null){
			map.put("o", u);
			return new ModelAndView("user/Home", map);
		}
		Company c=(Company)request.getSession().getAttribute("company");
		if(c!=null){
			map.put("o", c);
			return new ModelAndView("company/Home", map);
		}
		return new ModelAndView("login");
	}

	public ModelAndView getListTree(HttpServletRequest request,
			HttpServletResponse response) throws IllegalAccessException,
			InvocationTargetException {
		String code = request.getParameter("code");
		String type = request.getParameter("type");
		Map map = new HashMap();
		List list = null;
//		list = this.sysManager.getListTree(code, Integer.parseInt(type));
		getMessage(response, getJsonFromJSONArray(list));
		return null;
	}
	public ModelAndView getSelect(HttpServletRequest request,HttpServletResponse response) {
		String type=request.getParameter("type");
		String index=request.getParameter("index");
		String code=request.getParameter("code");
		Map map=new HashMap();
		map.put("type",type);
		List list=null;
		if(!select.containsKey(type)){
			Page page=getUniversalManager().findPageByCriteria(1, 1000, map, Code.class);
			if(page==null||page.getDataList()==null)return null;
			list=page.getDataList();
			select.put(type, list);
		}else list=(List)select.get(type);
		String option="";
		if(code==null||"".equals(code))
			for(Code c:(List<Code>)list){
				if(c.getCode().equals(index))
					option+="<option value='"+c.getCode()+"' selected>"+c.getName()+"</option>";
				else
					option+="<option value='"+c.getCode()+"'>"+c.getName()+"</option>";
			}
		else
			for(Code c:(List<Code>)list){
				if(c.getCode().equals(index))
					option+="<option value='"+c.getCode()+"' selected>["+c.getCode()+"] "+c.getName()+"</option>";
				else
					option+="<option value='"+c.getCode()+"'>["+c.getCode()+"] "+c.getName()+"</option>";
			}
		getMessage(response, option);
		return null;
	}
	//上传文件
	public ModelAndView getUpload(HttpServletRequest request,HttpServletResponse response) throws Exception{
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map fileMap=multipartRequest.getFileMap();
		String rootPath=request.getRealPath("/");
		String url="";
		for (Iterator it = multipartRequest.getFileNames(); it.hasNext();) {
			   String key = (String) it.next();
			   MultipartFile file = multipartRequest.getFile(key);
			   String path="upload\\"+FileUtil.filePath();
			   String fileName=FileUtil.fileName(file.getOriginalFilename());
			   File f=new File(rootPath+path);
			   if(!f.exists())f.mkdirs();
			   f=new File(rootPath+path+fileName);
			   file.transferTo(f);
			   System.out.println(rootPath+path+fileName);
			   url+="||"+path+fileName;
		}
		if(url.length()>0)url=url.substring(2);
		url=url.replace("\\", "/");
		getMessage(response, "{err:'',msg:'"+url+"'}");
		return null;
	}
	
}
